package com.hippo.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RouteLocatorConfig
 * @Description TODO 自定义网关路由配置
 * @Author tangxl
 * @create 2023-03-27 14:19
 **/
@Configuration
public class RouteLocatorConfig {

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocator){
        RouteLocatorBuilder.Builder routes = routeLocator.routes();
        return routes
                .route("path_route1", r -> r.path("/guonei")
                        .uri("http://news.baidu.com/guonei"))  //说明：访问localhost:9527/guonei地址会转发到http://news.baidu.com/guonei地址
                .route("path_route2",r->r.path("/guoji")
                        .uri("https://news.baidu.com/guoji"))
                .route("path_route3",r->r.path("/gateway")
                        .uri("https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/"))
                .route(p->
                        p.path("/lb/**") //lb: 请求地址中必须包含lb
                                .filters(f->f.stripPrefix(1)) //去掉第一个路径
                                .uri("lb://cloud-payment-service") //lb: 负载均衡指向地址
                )
                .route("edevp-org",r->r.path("/org/lb")
                        .uri("lb://edevp-org"))
                .route("edevp-store",r->r.path("/store/lb")
                        .uri("lb://edevp-store"))
                .route("edevp-auth",r->r.path("/auth/lb")
                        .uri("lb://edevp-auth"))
                .build();
    }
}
